/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */


package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbActivityAbstractRule;
import storage.util.StorageManager;

/**
 * 
 * @author wan
 */
public class ActivityAbstractHelper {

	/**
	 * 将清洗规则保存到LaUDMS数据库中
	 * 
	 * @param activityAbstractRule
	 *            要存储的模型对象
	 */
	public void saveActivityAbstractRule(
			DbActivityAbstractRule activityAbstractRule) {
		// StorageManager sm = new StorageManager(StorageParameter.db_keySpace,
		// StorageParameter.db_columnFamily_activityAbstractRule);
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_activityAbstractRule"));
		// sm.open();
		sm.setRowKey(activityAbstractRule.getRowKey());
		sm.put(DbActivityAbstractRule.CL_NAME, activityAbstractRule.getName());
		sm.put(DbActivityAbstractRule.CL_CREATETIME,
				activityAbstractRule.getCreateTime());
		sm.put(DbActivityAbstractRule.CL_PRODUCT,
				activityAbstractRule.getProduct());
		sm.put(DbActivityAbstractRule.CL_TAGLIST,
				activityAbstractRule.getTaglist());
		ArrayList<String> abstractRuleList = activityAbstractRule
				.getAbstractRuleList();
		for (int i = 0; i < abstractRuleList.size(); i++) {
			sm.put(DbActivityAbstractRule.CL_ACTIVITYABSTRACTRULE + "_" + i,
					abstractRuleList.get(i));
		}
		// sm.close();
	}

	/**
	 * 
	 * @param activityAbstractRule
	 * activityAbstractRule
	 * @param ruleValue
	 * ruleValue
	 */
	public void insertNewAbstractionRule(
			DbActivityAbstractRule activityAbstractRule, final String ruleValue) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_activityAbstractRule"));
		sm.setRowKey(activityAbstractRule.getRowKey());
		int ruleLength = activityAbstractRule.getAbstractRuleList().size();
		sm.put(DbActivityAbstractRule.CL_ACTIVITYABSTRACTRULE + "_"
				+ ruleLength, ruleValue);
	}

	/**
	 * 
	 * @param columnName
	 * columnName
	 * @param value
	 * columnName
	 * @return
	 */
	public List<DbActivityAbstractRule> getActivityAbstractRules(
			final String columnName, final String value) {
		List<DbActivityAbstractRule> result = new LinkedList<DbActivityAbstractRule>();
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_activityAbstractRule"));
		// sm.open();
		List<KeySlice> crs = sm.get(columnName, value);
		try {
			for (KeySlice ks : crs) {
				String rowKey = new String(ks.getKey());
				String name = null, createTime = null, product = null, taglist = null;
				ArrayList<String> abstractRuleList = new ArrayList<String>();
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbActivityAbstractRule.CL_NAME)) {
						name = columnsValue;
					} else if (columnsName
							.equals(DbActivityAbstractRule.CL_TAGLIST)) {
						taglist = columnsValue;
					} else if (columnsName
							.equals(DbActivityAbstractRule.CL_CREATETIME)) {
						createTime = columnsValue;
					} else if (columnsName
							.equals(DbActivityAbstractRule.CL_PRODUCT)) {
						product = columnsValue;
					} else if (columnsName
							.startsWith(DbActivityAbstractRule.CL_ACTIVITYABSTRACTRULE)) {
						abstractRuleList.add(columnsValue);
					}
				}
				DbActivityAbstractRule pActivityAbstractRule = new DbActivityAbstractRule(
						rowKey, name, product, taglist, abstractRuleList,
						createTime);
				result.add(pActivityAbstractRule);
			}
		} catch (Exception ex) {
			Logger.getLogger(ActivityAbstractHelper.class.getName()).log(
					Level.SEVERE, null, ex);
		}
		return result;
	}
/**
 * 
 * @return ArrayList<String>
 */
	public ArrayList<String> getAllActivityAbstractRuleName() {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_activityAbstractRule"));
		// sm.open();
		List<KeySlice> crs = sm.getAll();
		ArrayList<String> activityAbstractRuleNameList = new ArrayList<String>();
		try {
			for (KeySlice ks : crs) {
				String name = "";
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbActivityAbstractRule.CL_NAME)) {
						name = columnsValue;
					}
				}
				if (!activityAbstractRuleNameList.contains(name)
						&& name.length() > 0) {
					activityAbstractRuleNameList.add(name);
				}
			}
		} catch (Exception ex) {
			Logger.getLogger(ActivityAbstractHelper.class.getName()).log(
					Level.SEVERE, null, ex);
		}
		return activityAbstractRuleNameList;
	}
	/**
	 * 
	 * @param rowKey
	 * rowKey
	 */
	public void deleteActivityAbstractRule(final String rowKey) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_activityAbstractRule"));
		sm.delete(rowKey);
	}
}
